
/*
 * hdump.c
 *
 *
 * $Id: hdump.c,v 1.2 2005/11/18 22:14:38 ralf Exp $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "hex_print.h"

#define SUCCESS 1
#define FAILURE 0
#define TRUE    1
#define FALSE   0

int
hex_dump(const char *filename)
{
  int fd;
  int reading;
  ssize_t cc;
  size_t bytes_read = 0;
  char buffer[8192];

  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "ERROR: cannot open file '%s': %s\n", filename, strerror(errno));
    return FAILURE;
  } /* end if */

  printf("=== %s ===\n", filename);
  reading = TRUE;
  while (reading) {
    cc = read(fd, buffer, sizeof(buffer));
    if (cc > 0) {
      hex_print(stdout, buffer, cc, bytes_read);
      bytes_read += cc;
    } else if (cc == 0) {
      reading = FALSE;
    } else {
      fprintf(stderr, "ERROR: reading from file '%s': %s\n", filename, strerror(errno));
      close(fd);
      return FAILURE;
    } /* end if */
  } /* end while */

  close(fd);

  printf("%s: %d byte(s)\n\n", filename, bytes_read);

  return SUCCESS;
} /* end of hex_dump */


int
main(int argc, char *argv[])
{
  int i;

  for (i=1; i<argc; i++) {
    hex_dump(argv[i]);
  } /* end for */

  exit(0);
} /* end of main */
